package com.mashibing.disruptor;

import com.lmax.disruptor.EventTranslator;
import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.EventTranslatorTwoArg;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;

/**
 * @author: Yuanzt
 * @Date: 2021/4/7
 */
public class Main02 {

    public static void main(String[] args) {
        LongEventFactory factory = new LongEventFactory();
        int bufferSize = 1024;
        Disruptor<LongEvent> disruptor = new Disruptor<>(factory,bufferSize, DaemonThreadFactory.INSTANCE);
        disruptor.handleEventsWith(new LongEventHandler());
        disruptor.start();
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();

        //=========================================================================
        /*EventTranslator<LongEvent> translator = new EventTranslator<LongEvent>() {
            @Override
            public void translateTo(LongEvent event, long l) {
                event.set(8888L);
            }
        };
        ringBuffer.publishEvent(translator);
        */

        ringBuffer.publishEvent((e,l)->e.set(8888L));

        //=========================================================================
        /*EventTranslatorOneArg<LongEvent,Long> translator2 = new EventTranslatorOneArg<LongEvent, Long>() {
            @Override
            public void translateTo(LongEvent event, long sequence, Long l) {
                event.set(l);
            }
        };
        ringBuffer.publishEvent(translator2,7777L);
        */
        ringBuffer.publishEvent((e,s,l)->e.set(l),7777L);

        //=========================================================================
        /*EventTranslatorTwoArg<LongEvent,Long,Long> translator3 = new EventTranslatorTwoArg<LongEvent, Long, Long>() {
            @Override
            public void translateTo(LongEvent event, long sequence, Long l1, Long l2) {
                event.set(l1+l2);
            }
        };
        ringBuffer.publishEvent(translator3,1111L,2222L);*/
        ringBuffer.publishEvent((e,s,l1,l2)->e.set(l1+l2),2222L,3333L);
        //=========================================================================

    }
}
